﻿using System;
using System.Runtime.Remoting.Messaging;
using System.Web;

namespace Tools
{
    public class ContextDataStorer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value)
        {
            HttpContext currentContext = HttpContext.Current;
            if (currentContext == null)
            {
                CallContext.SetData(key, value);
            }
            else
            {
                currentContext.Items[key] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Get(string key)
        {
            HttpContext currentContext = HttpContext.Current;
            if (currentContext == null)
            {
                return CallContext.GetData(key);
            }
            else
            {
                return currentContext.Items[key];
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            Set(key, null);

            //HttpContext currentContext = HttpContext.Current;
            //if (currentContext == null)
            //{
            //    CallContext.SetData(key, null);
            //}
            //else
            //{
            //    currentContext.Items[key] = null;
            //}
        }
    }
}
